package ncc.wolfcode.luowowo.mgrsite.controller;


import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("/strategyDetail")
@Controller
public class StrategyDetailController {
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyService strategyService;
    @Reference
    private IStrategyCatalogService catalogService;
    @Reference
    private IStrategyThemeService themeService;
    @Reference
    private IStrategyTagService tagService;

    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo") StrategyDetailQuery qo) {
        PageInfo pageInfo = strategyDetailService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "strategyDetail/list";
    }

    @RequestMapping("/input")
    public String input(Model model,@ModelAttribute("qo") StrategyDetailQuery qo,Long id) {
        if(id!=null) {
            StrategyDetail strategyDetail = strategyDetailService.get(id);
            StrategyContent strategyContent=strategyDetailService.getContentById(id);
            strategyDetail.setStrategyContent(strategyContent);
            String tags = tagService.getTags(id);
            model.addAttribute("tags",tags);
            model.addAttribute("strategyDetail",strategyDetail);
        }
        List<Strategy> strategies = strategyService.list();
        model.addAttribute("strategies",strategies);
        List<StrategyTheme> themes = themeService.list();
        model.addAttribute("themes",themes);
        return "strategyDetail/input";
    }

    @RequestMapping("/getCatalogByStrategyId")
    @ResponseBody
    public List<StrategyCatalog> getCatalogByStrategyId(Long strategyId) {
        return catalogService.queryCatalogByStrategyId(strategyId);
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public AjaxResult saveOrUpdate(StrategyDetail strategyDetail,String tags) {
        strategyDetailService.saveOrUpdate(strategyDetail,tags);

        return AjaxResult.SUCCESS;
    }
}
